home *** CD-ROM | disk | FTP | other *** search
/ PC Graphics Unleashed / PC Graphics Unleashed.iso / ch20 / source / hist12.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-12  |  4.2 KB  |  236 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <graphics.h>
  5. #include <io.h>
  6. #include <malloc.h>
  7. #include <string.h>
  8. #include <math.h>
  9.  
  10.  
  11. #define BUFFERSIZE 128*128
  12.  
  13.  
  14. void main(int argc, char **argv)
  15. {
  16. int i,j,k,l;
  17. FILE *in;
  18. int startgray;
  19. int grays;
  20. short buffer[BUFFERSIZE];
  21. int filesize;
  22. int bpf;           /* buffers per file */
  23. unsigned int *histogram;
  24. int grdriver, grmode, grerror;
  25. float avg, temp;
  26. unsigned short disp[512];
  27. int ratio;
  28. char ticks[15];
  29. int tw;
  30.  
  31.  
  32.   /* Ensure proper syntax */
  33.   
  34.   if(argc != 4)
  35.     {
  36.     printf("Usage:: hist12 filename startgray numgray.\n\n");
  37.     exit(1);
  38.     }
  39.  
  40.   startgray = atoi(argv[2]);
  41.   grays = atoi(argv[3]);
  42.  
  43.   switch(grays)
  44.     {
  45.     case 4096:
  46.     case 2048:
  47.     case 1024:
  48.     case 512:
  49.     case 256:
  50.     case 128:
  51.     case 64:
  52.     case 32:
  53.       break;
  54.  
  55.     default:
  56.       printf("\nPlease use a power of 2 (128, 256, etc.)\n"); 
  57.       printf("for the number of gray values.\n\n");
  58.       exit(2);
  59.     }  
  60.  
  61.  
  62.   /* Allocate histogram storage space */
  63.  
  64.   histogram = (unsigned int *)malloc(grays * sizeof(int));
  65.   if(histogram == NULL)
  66.     {
  67.     printf("\nMemory allocation error.\n");
  68.     exit(3);
  69.     }
  70.   
  71.   
  72.   /* open input file */
  73.   
  74.   in = fopen(argv[1], "rb");
  75.   if(in == NULL)
  76.     {
  77.     printf("Can not open file: %s.\n\n", argv[1]);
  78.     exit(4);
  79.     }
  80.  
  81.   
  82.   /* Clear the histogram */
  83.  
  84.   for(i=0; i<grays; i++)
  85.     histogram[i] = 0;
  86.   
  87.   
  88.   /* Determine the number of disk reads needed */
  89.  
  90.   filesize = filelength( fileno(in) );
  91.   bpf = filesize / sizeof(buffer);
  92.  
  93.   
  94.   /* Calculate the histogram */
  95.   
  96.   for(i=0; i<bpf; i++)
  97.     {
  98.     fread(buffer, sizeof(buffer), 1, in);
  99.  
  100.     for(j=0; j<BUFFERSIZE; j++)
  101.       {
  102.       if(buffer[j] >= startgray) 
  103.         if(buffer[j] < startgray + grays)
  104.           histogram[ buffer[j] - startgray]++;
  105.       }
  106.     }
  107.  
  108.   fclose(in);
  109.  
  110.  
  111.   /* check graphics hardware */
  112.  
  113.   grdriver = DETECTX;
  114.   grmode = 0;
  115.   detectgraph(&grdriver, &grmode);
  116.   switch(grdriver)
  117.     {
  118.     case VESA256:
  119.     case ATI256:
  120.     case COMPAQ:
  121.     case TSENG3256:
  122.     case TSENG4256:
  123.     case GENOA5:
  124.     case GENOA6:
  125.     case OAK:
  126.     case PARADIS256:
  127.     case TECMAR:
  128.     case TRIDENT256:
  129.     case VIDEO7:
  130.     case VIDEO7II:
  131.       break;
  132.  
  133.     default:
  134.       printf("Unable to detect 256 color graphics adapter.\n");
  135.       exit(5);
  136.       break;
  137.     }
  138.   
  139.   
  140.   grmode = 1;
  141.   initgraph(&grdriver, &grmode, "");
  142.   grerror = graphresult();
  143.   if(grerror)
  144.     {
  145.     closegraph();
  146.     printf("Error %d initializing graphics mode.\n", grerror);
  147.     exit(6);
  148.     }
  149.  
  150.  
  151.   /* get the average histogram value */
  152.  
  153.   avg = 0.0;
  154.   j = 0;
  155.  
  156.   for(i=0; i<grays; i++)
  157.     {
  158.     avg += (float)histogram[i];
  159.     if(histogram[i] != 0)
  160.       j++;
  161.     }
  162.  
  163.   avg = avg/(float)j;
  164.  
  165.  
  166.   /* setup display array */
  167.  
  168.   if(grays >= 512)   
  169.     {
  170.     ratio = grays/512;
  171.  
  172.     for(i=0; i<512; i++)
  173.       {
  174.       temp = 0.0;
  175.       for(j=0; j<ratio; j++)
  176.         temp += (float)histogram[i*ratio + j] / (float)ratio; 
  177.       k = (int)floor( (double)(temp / avg * 200.0));
  178.       if(k > 400)
  179.         k = 400;
  180.       disp[i] = k;
  181.       }
  182.     }
  183.    else
  184.      {
  185.      ratio = 512/grays;
  186.      
  187.      for(i=0; i<grays; i++)
  188.        {
  189.        k = (int)floor( (double)(histogram[i] / avg * 200.0));
  190.        if(k > 400)
  191.          k = 400;
  192.        for(j=0; j<ratio; j++)
  193.          disp[i*ratio + j] = k;
  194.        }
  195.      }
  196.  
  197.  
  198.   /* display background */
  199.   
  200.   setbkcolor(31);     /* white */
  201.   cleardevice();
  202.   
  203.   setcolor(0);
  204.   rectangle(63,39, 640-64,480-39);
  205.  
  206.  
  207.   /* display histogram */
  208.   
  209.   setcolor(1);        /* blue */
  210.   for(i=0; i<512; i++)
  211.     line(64+i, 440, 64+i, 440 - disp[i]);
  212.   
  213.   
  214.   /* display tick marks and title */
  215.   
  216.   setcolor(0);
  217.   for(i=0; i<=8; i++)  
  218.     {
  219.     sprintf(ticks, "%d", i*grays/8 + startgray);
  220.     tw = textwidth(ticks);
  221.     outtextxy(64*(i+1)-tw/2+1, 450, ticks);
  222.     line(64*(i+1), 441, 64*(i+1), 444);
  223.     }
  224.  
  225.   tw = textwidth(argv[1]);
  226.   outtextxy(320-tw/2+1, 20, argv[1]);
  227.  
  228.  
  229.   /* wait for keystrike then exit */
  230.   
  231.   getch();
  232.   
  233.   closegraph();
  234.   free( (void *)histogram);
  235. }
  236.